package controller.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import factory.UserFactory;
import pojo.User;

@WebServlet("/showLookedUser")
public class ShowLookedUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			UserDao userDao = UserFactory.getInstance("userdaoimpl", UserDao.class);
			String searchWay = request.getParameter("searchWay");
			String keyword = request.getParameter("keyword");
			List<User> users = new ArrayList<User>();
			switch (searchWay) {
			case "uname":
				User user = userDao.getAppointUser(keyword);
				users.add(user);
				break;
			case "umessage":
				users = userDao.getUserByMessage(keyword);
				break;
			default:
				break;
			}
			request.setAttribute("users", users);
			request.getRequestDispatcher("view/showLookedUser.jsp").forward(request, response);
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}